package top.infopub.mgr.bpa.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.BizDataDao;
import top.infopub.mgr.bpa.dao.BizDataDetailDao;
import top.infopub.mgr.bpa.domain.BizDataDomain;
import top.infopub.mgr.bpa.service.BizDataService;
import top.infopub.mgr.bpa.vo.BizDataVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.bpa.BizData;
import top.infopub.model.bpa.BizDataDetail;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class BizDataServiceImpl extends GenericServiceImpl<BizData, String> implements
		BizDataService {

	@Autowired
	private BizDataDao bizDataDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;

    @Autowired
    private BizDataDetailDao bizDataDetailDao;
	
	@Override
	public GenericDao<BizData, String> getDao() {
		return bizDataDao;
	}

    @Override
    public Page<BizDataDomain> selectPageByDomainWhere(Page<BizDataDomain> page,
                                                       BizDataVo bizDataVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BizDataDomain>)bizDataDao.selectByDomainWhere(bizDataVo);
    }

    @Override
    public Page<BizDataDomain> selectMyCheckByDomainWhere(Page<BizDataDomain> page,
                                                          BizDataVo bizDataVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<BizDataDomain>)bizDataDao.selectMyCheckByDomainWhere(bizDataVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BizDataVo bizData, List<BizDataDetail> bizDataDetails) {
        //插入记录
        bizDataDao.insertById(bizData);
        
        //批量插入详情
        if(CollectionUtils.isNotEmpty(bizDataDetails)){
            bizDataDetailDao.insertByBatch(bizDataDetails);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_BIZ_DATA;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, bizData.getId(), bizData.getProcessId(), null);
    }

    @Override
    public BizDataVo queryById(String id) {
        return bizDataDao.queryById(id);
    }

}